package com.ones.sdp.kit.log.event;

import com.ones.sdp.kit.log.model.dto.OnesLogDto;
import com.ones.sdp.kit.log.service.OnesLogService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步监听日志事件
 * @author Clark
 * @version 2025-02-16
 */
@Slf4j
@RequiredArgsConstructor
public class OnesLogListener {
	private final OnesLogService logService;

	/**
	 * 异步保存日志信息
	 *
	 * @param event 触发的 {@link OnesLogEvent} 事件对象，包含日志相关信息。
	 * @throws Exception
	 */
	@SneakyThrows
	@Async
	@EventListener(OnesLogEvent.class)
	public void saveLog(OnesLogEvent event) {
		// 获取日志信息
		OnesLogDto source = (OnesLogDto) event.getSource();
		// 保存日志
		this.logService.saveLog(source);
	}
}